package com.yt.base.controller.order;


import com.yt.core.MsgBean;
import com.yt.core.dao.filter.DynamicSQLFilterImpl;
import com.yt.core.dao.filter.QueryFilter;
import com.yt.order.api.interfaces.WxReserveService;
import com.yt.order.api.model.WxReserve;
import com.yt.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/wx/reserve")
public class WxReserveController {

    @Autowired
    private WxReserveService wxReserveService;

    @RequestMapping("/list")
    public ModelAndView list(@RequestParam(value="name", required = false) String name,
                             @RequestParam(value="mobile", required = false) String mobile,
                             @RequestParam(value="reserveStartDate", required = false) String reserveStartDate,
                             @RequestParam(value="reserveEndDate", required = false) String reserveEndDate,
                             HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("order/wxreserve/list");
        QueryFilter filter = new DynamicSQLFilterImpl();
        filter.addFilter(request.getParameterMap());
        filter.addFilter("Q_name_S_EQ", name);
        filter.addFilter("Q_mobile_S_EQ", mobile);
        try{
            filter.addFilter("Q_reserveDate_L_GE",String.valueOf(DateUtils.parseDate(reserveStartDate).getTime()/1000));
        }catch(Exception ex){}
        try{
            filter.addFilter("Q_reserveDate_L_LT", String.valueOf(DateUtils.parseDate(reserveEndDate).getTime() / 1000));
        }catch(Exception ex){}
        filter.setSortField("id", true);
        List<WxReserve> list = wxReserveService.find(filter);
        mav.addObject("name", name);
        mav.addObject("mobile", mobile);
        mav.addObject("reserveStartDate", reserveStartDate);
        mav.addObject("reserveEndDate", reserveEndDate);
        mav.addObject("list", list);
        mav.addObject("start", filter.getPagingBean().getStart());
        mav.addObject("limit", filter.getPagingBean().getPageSize());
        return mav;
    }

    @RequestMapping("/changeStatus")
    @ResponseBody
    public Object changeStatus(@RequestParam(value = "ids", required = true) String ids,
                               @RequestParam(value = "status", required = true) String status) {
        MsgBean msg = new MsgBean();
        try {
            String[] idArr = ids.split(",");
            WxReserve reserve = null;
            for (int i = 0; i < idArr.length; i++) {
                reserve = wxReserveService.get(Long.parseLong(idArr[i]));
                reserve.setStatus(status);
                wxReserveService.update(reserve);
            }
        } catch (Exception ex) {
            return msg.failure("删除订单出错，"+ ex.getMessage());
        }
        return msg.returnMsg();
    }
}
